//
//  BatteryView.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/4.
//

#import "BatteryView.h"
#import "Public.h"

@implementation BatteryView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.titleLB = [[UILabel alloc] init];
    self.titleLB.font = FONT_BODY_15;
    self.titleLB.textAlignment = NSTextAlignmentCenter;
    self.titleLB.textColor = COLOR_NOT_SELECT;
    self.titleLB.text = @"剩余电量";
    [self addSubview:self.titleLB];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.left.top.right.equalTo(self).offset(0);
    }];
    
    self.batteryLB = [[UILabel alloc] init];
    self.batteryLB.font = FONT_PRIMARY_TITLE;
    self.batteryLB.textColor = COLOR_SIMPLEBLACK;
    self.batteryLB.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.batteryLB];
    [self.batteryLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.bottom.right.equalTo(self).offset(0);
    }];

    self.batteryIMV = [[UIImageView alloc] init];
    self.batteryIMV.image = [UIImage imageNamed:@"battery"];
    [self addSubview:self.batteryIMV];
    [self.batteryIMV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.batteryLB.mas_top).offset(5);
        make.size.mas_equalTo(CGSizeMake(20, 30));
        make.centerX.mas_equalTo(self.mas_centerX);
    }];

}

- (void)setBattery:(CGFloat)battery {
    _battery = battery;
    self.batteryLB.text = [NSString stringWithFormat:@"%.0f%%",battery*100];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    self.backgroundColor =[UIColor whiteColor];
    
    CGFloat originX = rect.size.width / 2;
    CGFloat originY = rect.size.height / 2;

    // 背景圆环
    UIBezierPath *bgpath = [[UIBezierPath alloc] init];
    bgpath.lineWidth = 4;
    [rgbHex(0xe5e5e5, 1.0) set];
    bgpath.lineCapStyle = kCGLineCapRound;
    bgpath.lineJoinStyle = kCGLineJoinRound;
    CGFloat radius = MIN(originX, originY) - 2;
    [bgpath addArcWithCenter:CGPointMake(originX, originY) radius:radius startAngle:M_PI*3/4 endAngle:M_PI*2+M_PI/4 clockwise:YES];
    [bgpath stroke];
    
    // 1. 还是添加一个圆弧路径
    // 获取当前context
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 设置线的宽度
    CGContextSetLineWidth(ctx, 4);
    // 设置线条端点为圆角
    CGContextSetLineCap(ctx, kCGLineCapRound);
    // 设置画笔颜色
    CGContextSetFillColorWithColor(ctx, rgbHex(0xe5e5e5, 1.0).CGColor);
    // 计算半径
    CGFloat radius1 = MIN(originX, originY) - 2;
    // 逆时针画一个圆弧
    CGContextAddArc(ctx, originX, originY, radius1, M_PI*2+M_PI/4-(M_PI*2-M_PI/2)*self.battery, M_PI*2+M_PI/4,  0);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    NSArray *colorArr = @[
            (id)rgbHex(0x1ed6c2,1.0).CGColor,
            (id)rgbHex(0x00dea1,1.0).CGColor
        ];

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colorArr, NULL);
    CGColorSpaceRelease(colorSpace);
    colorSpace = NULL;

    CGContextReplacePathWithStrokedPath(ctx);
    CGContextClip(ctx);
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, rect.size.height), CGPointMake(rect.size.width, 0), 1);
    CGGradientRelease(gradient);
}

@end
